import { defineStore } from 'pinia';
import { BreadcrumbItem } from '@/types/app';

export const useBreadcrumbStore = defineStore('breadcrumb', {
  state: () => ({
    breadcrumbList: [] as BreadcrumbItem[],
  }),
  actions: {
    // 设置完整的面包屑
    setBreadcrumb(list: BreadcrumbItem[]) {
      this.breadcrumbList = list;
    },

    // 添加面包屑项
    addBreadcrumb(item: BreadcrumbItem) {
      this.breadcrumbList.push(item);
    },

    // 根据路径删除面包屑项
    removeBreadcrumb(path: string) {
      const index = this.breadcrumbList.findIndex(
        (item) => item.path.path === path,
      );
      if (index !== -1) {
        this.breadcrumbList.splice(index, 1);
      }
    },

    // 清空面包屑
    clearBreadcrumb() {
      this.breadcrumbList = [];
    },
  },
});
